package uk.ac.lkl.takteen.energyapp;

import java.util.Calendar;

import uk.ac.lkl.takteen.energyapp.data.EnergyAppDatabaseMetadata;
import uk.ac.lkl.takteen.energyapp.data.EnergyAppProvider;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;

public class EnergyAppActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// set num challenges and pledges from the database
		final Button viewCurrentChallengesButton = (Button) findViewById(R.id.viewCurrentChallengesButton);
		final Button viewCurrentPledgesButton = (Button) findViewById(R.id.viewCurrentPledgesButton);

		int numChallenges = getNumReceivedChallenges();
		int numActivePledges = getNumActivePledges();

		// TO DO add also on resume
		viewCurrentChallengesButton.setText("You have " + numChallenges
				+ " challenges");
		viewCurrentPledgesButton.setText("You have " + numActivePledges
				+ " active pledges");

		// add listeners to the buttons
		final Button createChallengeButton = (Button) findViewById(R.id.createChallengeButton);
		createChallengeButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_INSERT,
						EnergyAppProvider.USER_CHALLENGES_DIR,
						EnergyAppActivity.this, ChallengeEditor.class);
				startActivity(intent);
			}
		});
		final Button sendChallengeButton = (Button) findViewById(R.id.sendChallengeButton);
		sendChallengeButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_VIEW,
						EnergyAppProvider.USER_CHALLENGES_DIR,
						EnergyAppActivity.this, Challenges.class);
				startActivity(intent);
			}
		});
		final Button makePledgeButton = (Button) findViewById(R.id.makePledgeButton);
		makePledgeButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_INSERT,
						EnergyAppProvider.USER_PLEDGES_DIR,
						EnergyAppActivity.this, PledgeEditor.class);
				startActivity(intent);
			}
		});
		final Button sharePledgeButton = (Button) findViewById(R.id.sharePledgeButton);
		sharePledgeButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_VIEW,
						EnergyAppProvider.USER_PLEDGES_DIR,
						EnergyAppActivity.this, Pledges.class);
				startActivity(intent);
			}
		});
		viewCurrentChallengesButton
				.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent(Intent.ACTION_VIEW,
								EnergyAppProvider.RECEIVED_CHALLENGES_DIR,
								EnergyAppActivity.this, Challenges.class);
						intent.addFlags(1);
						startActivity(intent);
					}
				});
		viewCurrentPledgesButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_VIEW,
						EnergyAppProvider.USER_PLEDGES_DIR,
						EnergyAppActivity.this, Pledges.class);
				startActivity(intent);
			}
		});

	}

	public void onResume() {
		super.onResume();
		// set num challenges and pledges from the database
		final Button viewCurrentChallengesButton = (Button) findViewById(R.id.viewCurrentChallengesButton);
		final Button viewCurrentPledgesButton = (Button) findViewById(R.id.viewCurrentPledgesButton);

		int numChallenges = getNumReceivedChallenges();
		int numActivePledges = getNumActivePledges();

		viewCurrentChallengesButton.setText("You have " + numChallenges
				+ " challenges");
		viewCurrentPledgesButton.setText("You have " + numActivePledges
				+ " active pledges");

	}

	public int getNumReceivedChallenges() {
		Cursor cursor = managedQuery(EnergyAppProvider.RECEIVED_CHALLENGES_DIR,
				EnergyAppDatabaseMetadata.PROJECTION_RECEIVED_CHALLENGES, null,
				null, null);
		return cursor.getCount();
	}

	public int getNumActivePledges() {
		Calendar cal = Calendar.getInstance();
		int iYear = cal.get(Calendar.YEAR);
		int iMonth = cal.get(Calendar.MONTH);
		int iDay = cal.get(Calendar.DAY_OF_MONTH);

		String selectionNotExpired = EnergyAppDatabaseMetadata.EXPIRATION_YEAR
				+ ">" + iYear + " OR ("
				+ EnergyAppDatabaseMetadata.EXPIRATION_YEAR + "=" + iYear
				+ " AND " + EnergyAppDatabaseMetadata.EXPIRATION_MONTH + ">"
				+ iMonth + ")" + " OR ("
				+ EnergyAppDatabaseMetadata.EXPIRATION_YEAR + "=" + iYear
				+ " AND " + EnergyAppDatabaseMetadata.EXPIRATION_MONTH + "="
				+ iMonth + " AND " + EnergyAppDatabaseMetadata.EXPIRATION_DAY
				+ ">=" + iDay + ")";

		Cursor cursor = managedQuery(EnergyAppProvider.USER_PLEDGES_DIR,
				EnergyAppDatabaseMetadata.PROJECTION_USER_PLEDGES,
				selectionNotExpired, null, null);
		return cursor.getCount();
	}
}